package com.zhb.zhbojbackendcommentservice.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhb.zhbojbackendcommentservice.mapper.CommentMapper;
import com.zhb.zhbojbackendcommentservice.service.CommentService;
import com.zhb.zhbojbackendcommon.common.ErrorCode;
import com.zhb.zhbojbackendcommon.exception.BusinessException;
import com.zhb.zhbojbackendmodel.model.dto.comment.CommentAddRequest;
import com.zhb.zhbojbackendmodel.model.entity.Comment;
import com.zhb.zhbojbackendmodel.model.entity.User;
import com.zhb.zhbojbackendmodel.model.vo.CommentVO;
import com.zhb.zhbojbackendserviceclient.service.UserFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    @Resource
    private UserFeignClient userFeignClient;

    @Override
    public boolean addComment(CommentAddRequest request, User loginUser) {
        Comment comment = new Comment();
        comment.setQuestionId(request.getQuestionId());
        comment.setContent(request.getContent());
        comment.setUserId(loginUser.getId());
        comment.setParentId(request.getParentId());
        return this.save(comment);
    }

    @Override
    public Page<CommentVO> listCommentByQuestionId(Long questionId, long pageNum, long pageSize) {
        Page<Comment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getQuestionId, questionId)
                .eq(Comment::getIsDelete, 0)
                .isNull(Comment::getParentId) // 只查顶级评论
                .orderByDesc(Comment::getCreateTime);
        this.page(page, queryWrapper);

        List<CommentVO> commentVOList = page.getRecords().stream().map(comment -> {
            CommentVO vo = new CommentVO();
            BeanUtils.copyProperties(comment, vo);
            User user = userFeignClient.getById(comment.getUserId());
            vo.setUserVO(userFeignClient.getUserVO(user));
            return vo;
        }).collect(Collectors.toList());

        // 加载子评论
        for (CommentVO vo : commentVOList) {
            List<CommentVO> children = getChildren(vo.getId());
            vo.setChildren(children);
        }

        Page<CommentVO> voPage = new Page<>();
        BeanUtils.copyProperties(page, voPage);
        voPage.setRecords(commentVOList);
        return voPage;
    }

    private List<CommentVO> getChildren(Long parentId) {
        List<Comment> children = this.lambdaQuery()
                .eq(Comment::getParentId, parentId)
                .eq(Comment::getIsDelete, 0)
                .orderByAsc(Comment::getCreateTime)
                .list();

        return children.stream().map(comment -> {
            CommentVO vo = new CommentVO();
            BeanUtils.copyProperties(comment, vo);
            User user = userFeignClient.getById(comment.getUserId());
            vo.setUserVO(userFeignClient.getUserVO(user));
            return vo;
        }).collect(Collectors.toList());
    }
}
